// pages/cart/index.js
// import Toast from '@vant/weapp/toast/toast';
// import api from '../../api/axios'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shoplist: [],
        priceAll: "0",
        type: true,
        total: "",
        num: 0,
        resultsumbit: [],
        allchecked: false,
        resourceURL: '',
        // reduce: 0
    },
    changecheck(event) {
        this.setData({
            allchecked: event.detail,
        });
        var arr = this.data.shoplist[0].products.map((e) => {
            return {
                ...e,
                checked: this.data.allchecked,
            };
        });
        this.setData({
            "shoplist[0].products": arr
        })
        this.countprice();
    },
    //计算价格
    countprice() {
        var newarr = this.data.shoplist[0].products.filter((v) => {
            return v.checked && v.is_validity;
        });
        // console.log(newarr);
        var num = 0;
        // var reduce = 0;
        for (let index = 0; index < newarr.length; index++) {
            // var pricearr = newarr[index].price_list.filter((v) => {
            //     return newarr[index].quantity >= v.sale_number;
            // });
            // console.log(newarr[index].quantity,newarr[index].price,newarr[index].mprice);
            var price = newarr[index].mprice * newarr[index].quantity;
            // var arr = 'shoplist[0].products[' + index + '].sale_price'
            // this.setData({
            //     [arr]: pricearr[0].trade_price,
            // })
            num = num + price;
            
            
            // console.log(num,"------",reduce);
        }
        
        // console.log(price, '1');
        this.setData({
            num: newarr.length,
            priceAll: num,
        
        })
        // console.log(this.data.priceAll, '1');
    },

    changenum(i) {
        // console.log(1);
        var value = i.currentTarget.dataset.item
        // console.log(i.currentTarget.dataset.item);
        wx.http.post(wx.api.usereditcart, {
                product_id: value.product_id,
                quantity: value.quantity,
                stock_id: value.p_s_id,
            })
            .then((res) => {
                // console.log(this.data.shoplist);
                // console.log('res',res);
                if (res.code == 1) {
                    // console.log(this.data.shoplist);
                    var arr = 'shoplist[0].products[' + i.currentTarget.dataset.index + '].quantity'
                    this.setData({
                        [arr]: i.detail,
                    });
                    this.countprice()
                } else {
                    wx.showToast({
                      title: res.msg,
                      icon:"error"
                    })
                }
            });
    },
    btong() {
        var newarr = this.data.shoplist[0].products.filter((v) => {
            return v.checked && v.is_validity;
        });
        for (let index = 0; index < newarr.length; index++) {
            this.data.resultsumbit.push(newarr[index].cart_id);
        }
        console.log(this.data.resultsumbit);
        if (this.data.resultsumbit.length == 0) {
            wx.showToast({
                title:"请选择一项",
                icon:"error"
              })
            return;
        }
        if (this.data.type) {
            wx.http.post(wx.api.settlement, {
                    cart_ids: this.data.resultsumbit,
                })
                .then((res) => {
                    if (res.code == 1) {
                        var cardid = this.data.resultsumbit.join(",");
                        wx.navigateTo({
                            url: '/pages/settlement/index?cart_ids=' + cardid,
                        })
                        console.log(cardid);
                        this.setData({
                            priceAll: "0",
                            total: "",
                            num: 0,
                            resultsumbit: [],
                            allchecked: false,
                        })
                    } else {
                        wx.showToast({
                            title: res.msg,
                            icon:"error"
                          })
                        
                    }
                    this.data.resultsumbit = [];
                });
        } else {
            console.log(this.data.resultsumbit);
            wx.showModal({
                title: '提示',
                content: '确认删除',
                confirmColor: "#B60102",
                complete: (res) => {
                    if (res.confirm) {
                        wx.http.post(wx.api.userdelcart, {
                                cart_ids: this.data.resultsumbit,
                            },'application/json')
                            .then((res) => {
                                if (res.code == 1) {
                                    this.getlist();
                                    this.data.priceAll = 0;
                                    this.data.allchecked = false;
                                    this.data.num = 0;
                                } else {
                                    wx.showToast({
                                        title: res.msg,
                                        icon:"error"
                                      })
                                }
                                this.data.resultsumbit = [];
                            });
                    }
                }
            })
        }
    },
    changetype() {
        this.setData({
            type: !this.data.type,
            priceAll: 0,
            num: 0,
            allchecked: false,
            resultsumbit: [],
        })
        this.getlist();
    },
    goodscheck(event) {
        var arr = 'shoplist[0].products[' + event.currentTarget.dataset.index + '].checked'
        this.setData({
            [arr]: event.detail,
        });
        this.countprice();
        var num = this.data.shoplist[0].products.filter((v) => {
            return v.is_validity;
        }).length;
        if (num == this.data.num) {
            this.setData({
                allchecked: true
            })
        } else {
            this.setData({
                allchecked: false
            })
        }
    },

    getlist() {
        wx.http.get(wx.api.usercartlist, {
                page: 1,
            }, true)
            .then((res) => {
                // console.log(res);
                if (res.code == 1) {
                    this.setData({
                        total: res.data.total
                    })
                    var newarr = res.data.data
                    // console.log(newarr);
                    if (newarr != "") {
                        newarr[0].products =
                            res.data.data[0].products.map((e) => {
                                return {
                                    ...e,
                                    checked: false,
                                };
                            });
                        this.setData({
                            shoplist: newarr
                        })
                    } else {
                        var arr = [{
                            products: []
                        }]
                        this.setData({
                            shoplist: arr
                        })
                    }
                }

            });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            resourceURL: `${wx.api.host}/`
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {


        this.getlist();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})